package com.zhuiyun.project.api.system.post.controller;


import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.system.post.entity.SysPost;
import com.zhuiyun.project.api.system.post.page.TableDataInfo;
import com.zhuiyun.project.api.system.post.service.ISysPostService;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.constants.CommonConstants;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 岗位信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController
{
    @Autowired
    private ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @ApiOperation(value = "获取岗位列表")
    @OptLog(modular = "岗位管理", type = CommonConstants.SELECT, describe = "获取岗位列表")
    @GetMapping("/list")
    public CommonResult list(SysPost post, PageRequest pageRequest)
    {
        return postService.selectPostList(post,pageRequest);
    }

////    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('system:post:export')")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysPost post)
//    {
//        List<SysPost> list = postService.selectPostList(post);
//        ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
//        util.exportExcel(response, list, "岗位数据");
//    }

    /**
     * 根据岗位编号获取详细信息
     */
    @ApiOperation(value = "根据岗位编号获取详细信息")
    @OptLog(modular = "岗位管理", type = CommonConstants.SELECT, describe = "根据岗位编号获取详细信息")
    @GetMapping(value = "/{postId}")
    public CommonResult getInfo(@PathVariable Long postId)
    {
        return CommonResult.ok(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @ApiOperation(value = "新增岗位")
    @OptLog(modular = "岗位管理", type = CommonConstants.ADD, describe = "新增岗位")
    @PostMapping
    public CommonResult add(@Validated @RequestBody SysPost post)
    {
        if (!postService.checkPostNameUnique(post))
        {
            return CommonResult.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (!postService.checkPostCodeUnique(post))
        {
            return CommonResult.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(getUsername());
        return CommonResult.ok(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @ApiOperation(value = "修改岗位")
    @OptLog(modular = "岗位管理", type = CommonConstants.UPDATE, describe = "修改岗位")
    @PutMapping
    public CommonResult edit(@Validated @RequestBody SysPost post)
    {
        if (!postService.checkPostNameUnique(post))
        {
            return CommonResult.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (!postService.checkPostCodeUnique(post))
        {
            return CommonResult.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(getUsername());
        return CommonResult.ok(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @ApiOperation(value = "删除岗位")
    @OptLog(modular = "岗位管理", type = CommonConstants.DELETE, describe = "删除岗位")
    @DeleteMapping("/{postIds}")
    public CommonResult remove(@PathVariable Long[] postIds)
    {
        return CommonResult.ok(postService.deletePostByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    @ApiOperation(value = "获取岗位选择框列表")
    @OptLog(modular = "用户管理", type = CommonConstants.SELECT, describe = "获取岗位选择框列表")
    public CommonResult optionselect()
    {
        List<SysPost> posts = postService.selectPostAll();
        return CommonResult.ok(posts);
    }
}
